﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace Ledict.Utility.Extension
{
    #region 枚举扩展方法
    /// <summary>
    /// 描述：枚举扩展方法
    /// 时间：2012-11-20
    /// 作者：张鹏
    /// </summary>
    public static class EnumExtensions
    {
        #region 获取枚举变量值的Description属性
        /// <summary>
        /// 获取枚举变量值的Description属性
        /// </summary>
        /// <param name="obj">枚举变量</param>
        /// <param name="isTop">是否改变为返回该类、枚举类型的头 Description 属性，而不是当前的属性或枚举变量值的 Description 属性</param>
        /// <returns>如果包含 Description 属性，则返回 Description 属性的值，否则返回枚举变量值的名称</returns>
        public static string GetEnumDescription(this object obj, bool isTop)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            try
            {
                Type enumType = obj.GetType();
                if (!enumType.IsEnum)
                {
                    throw new ArgumentException("enumItem requires a Enum ");
                }
                DescriptionAttribute dna = null;
                if (isTop)
                {
                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));
                }
                else
                {
                    FieldInfo fi = enumType.GetField(System.Enum.GetName(enumType, obj));
                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(
                       fi, typeof(DescriptionAttribute));
                }
                if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
                    return dna.Description;
            }
            catch
            {
            }
            return obj.ToString();


            //var enumType = typeof(TEnum);
            //if (!enumType.IsEnum)
            //{
            //    throw new ArgumentException("enumItem requires a Enum ");
            //}

            //string enumName = System.Enum.GetName(typeof(TEnum), (TEnum)value);


            //object[] objs = enumType.GetField(enumName).GetCustomAttributes(typeof(DescriptionAttribute), true);

            //if (objs == null || objs.Length == 0)
            //{


            //    return string.Empty;
            //}
            //else
            //{
            //    DescriptionAttribute attr = objs[0] as DescriptionAttribute;

            //    return attr.Description;
            //}

            //return enumItem;
        }
        #endregion

        #region 获取指定枚举类型的List
        /// <summary>
        /// 获取指定枚举类型的List
        /// </summary>
        /// <typeparam name="T">泛型（枚举具体类型）</typeparam>
        /// <param name="src">枚举</param>
        /// <returns>list</returns>
        public static IList<EnumItem> ToEnumList<T>(this T src)
        {
            //判断是否是枚举类型
            if (!src.GetType().IsEnum) return null;

            var list = new List<EnumItem>();
            foreach (var item in System.Enum.GetValues(typeof(T)))
            {
                list.Add(new EnumItem
                {
                    Value = ((int)item).ToString(),
                    Name = System.Enum.GetName(typeof(T), item),
                    CnName = item.GetEnumDescription(false)
                });

            }
            return list;
        }
        #endregion
    }
    #endregion

    #region 枚举值类
    /// <summary>
    /// 枚举值类
    /// </summary>
    /// <remarks>
    /// Create Date:2014-04-13
    /// Create Author:Zhangpeng
    /// </remarks>
    public class EnumItem
    {
        /// <summary>
        /// 枚举名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 枚举中文名称
        /// </summary>
        public string CnName { get; set; }

        /// <summary>
        /// 枚举值
        /// </summary>
        public string Value { get; set; }
    }
    #endregion

}
